package org.example;

import java.util.StringJoiner;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class TreeMap_Demo2 {
    public static void main(String[] args) {
        //根据字符串aababcabcdabcde 统计成a(5)b(4)c(3)d(2)e(1)

        String s="aababcabcdabcde";
        TreeMap<Character,Integer> tm=new TreeMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c=s.charAt(i);
            if(tm.containsKey(c)){
                int count=tm.get(c);
                count++;
                tm.put(c,count);
            }else{
                tm.put(c,1);
            }
        }
        System.out.println(tm);
        tm.forEach((k,v)->{
            System.out.print(k+"("+v+")");
        });
        System.out.println();
        //StringBuilder
        System.out.println("---------1");
        StringBuilder sb=new StringBuilder();
        tm.forEach((k,v)->{
            sb.append(k).append("(").append(v).append(")");
        });
        System.out.println(sb);
        //StringJoiner
        System.out.println("---------2");
        //括号内三个参数：分隔符，前缀，后缀
        StringJoiner sj=new StringJoiner("","","");
        tm.forEach((k,v)->{
            sj.add(k+"("+v+")");
        });
        System.out.println(sj);
        System.out.println("---------3");
        //标准匿名内部类
        tm.forEach(new BiConsumer<Character,Integer>(){
            @Override
            public void accept(Character k, Integer v) {
                System.out.print(k+"("+v+")");
            }
        });

    }
}
